local yingdao = fk.CreateSkill{
  name = "dajubei__yingdao",
}

Fk:loadTranslationTable{
  ["dajubei__yingdao"] = "应道",
  [":dajubei__yingdao"] = "当你于回合外放弃使用基本牌后，则你下一次需要使用此牌名的基本牌时，可视为使用之。",

  ["@$dajubei__yingdao"] = "应道",

  ["$dajubei__yingdao1"] = "",
  ["$dajubei__yingdao2"] = "",
  ["$dajubei__yingdao3"] = "",
  ["$dajubei__yingdao4"] = "",
}

local U = require "packages/utility/utility"

---@return string[]
local getAllBasicCardNames = function ()
  local names = {}
  table.forEach(Fk:getAllCardIds(), function (id)
    if Fk:getCardById(id).type == Card.TypeBasic then
      table.insertIfNeed(names, Fk:getCardById(id).trueName)
    end
  end)
  return names
end

yingdao:addEffect("viewas", {
  mute = true,
  anim_type = "switch",
  card_num = 0,
  card_filter = Util.FalseFunc,
  pattern = ".|.|.|.|.|basic",
  interaction = function (self, player)
    local all_choices = getAllBasicCardNames()
    local choices = table.filter(all_choices, function (name)
      return table.contains(player:getTableMark("@$dajubei__yingdao"), Fk:cloneCard(name).trueName)
    end)
    return UI.CardNameBox { choices = choices, all_choices = all_choices }
  end,
  view_as = function(self, cards)
    if #cards ~= 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = yingdao.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:removeTableMark(player, "@$dajubei__yingdao", use.card.name)
    room:notifySkillInvoked(player, yingdao.name)
    player:broadcastSkillInvoke(yingdao.name, math.random(3,4))
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("@$dajubei__yingdao") > 0
  end,
  enabled_at_response = function(self, player, response)
    return #player:getTableMark("@$dajubei__yingdao") > 0 and not response
  end,
})

yingdao:addEffect(fk.AfterAskForCardUse, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(yingdao.name) and target == player
    and player.room.current ~= player and not data.isResponse then
      local patterns = tostring(Exppattern:Parse(data.pattern)):split("|")
      local names = table.filter(patterns[5]:split(","), function (n)
        return table.contains(getAllBasicCardNames(), n)
      end)
      if #names > 0 then
        event:setCostData(self, names)
        return true
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player:broadcastSkillInvoke(yingdao.name, math.random(1,2))
    table.forEach(event:getCostData(self), function (name)
      if not data.result then
        player.room:addTableMarkIfNeed(player, "@$dajubei__yingdao", name)
      else
        player.room:removeTableMark(player, "@$dajubei__yingdao", name)
      end
    end)
  end
})

return yingdao